<template>
  <div>
    <el-form ref="applyForm" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="领用人" prop="requirePerson">
        <el-select v-model="formItem.requirePerson" filterable clearable placeholder="请选择领用人">
          <el-option v-for="(item) in persons" :key="item.username" :label="item.name" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-row>
        <el-col :span="24">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>确认领取设备</span>
            </div>
            <div>
              <el-table
                :loading="loading"
                :data="insideTableData"
              >
                <el-table-column v-for="column in insideColumns" :key="column.key" :label="column.title" :prop="column.key" :sortable="column.sortable">
                  <template slot-scope="scope">
                    <column-render :render="column.render" :row="scope.row" :column="column" />
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <br>
      <el-row>
        <el-col :span="24">
          <SpareConsumeTK :iseable="true" :work-order-num="workOrderNum" :task-id="taskId" :require-person="formItem.requirePerson" @close="close" />
        </el-col>
      </el-row>
    </el-form>

  </div>
</template>
<script>
import databind from '@neu/biz-core/mixins/databind'
import { list as userList } from '@neu/auth-ui/api/user'
import { confirmUser } from '@neu/dcam-ui/api/spareconsume'
import SpareConsumeTK from '@neu/dcam-ui/views/workOrder/forms/spareconsume/spareConsumeTK'
import {
  listStockSelect
} from '@neu/dcam-ui/api/spareconsume'
const formItem = {
  requirePerson: null
}
export default {
  components: {
    SpareConsumeTK
  },
  mixins: [databind],
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      r: listStockSelect,
      persons: [],
      formItem: {
        ...formItem
      },
      insideColumns: [
        {
          title: '货品编号',
          key: 'storckNumber'
        }, {
          title: '备品备件分类',
          key: 'spareTypeName'
        },
        {
          title: '规格型号',
          key: 'specification'
        },
        {
          title: 'SN',
          key: 'sn'
        },
        {
          title: '存放库房',
          key: 'storehouseName'
        },
        {
          title: '单价',
          key: 'unitPrice'
        },
        {
          title: '货品来源',
          key: 'source',
          render: (h, params) => {
            let sourceDesc = ''
            if (params.row.source === 'new') {
              sourceDesc = '合同采购'
            } else if (params.row.source === 'back') {
              sourceDesc = '退库利旧'
            }
            return h('span', [
              h('el-tag', sourceDesc)
            ])
          }
        }
      ],
      rules: {
        requirePerson: [{ required: true, message: '领用人不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    const parameter = {
      's_workOrderNum': this.workOrderNum
    }
    this.filter(parameter)
    confirmUser(this.workOrderNum).then(res => {
      this.formItem.requirePerson = res.data
    })
  },
  mounted() {
    userList({ 's_roleCode': 'worker' }).then(res => {
      this.persons = res.data.map(e => { return { name: e.name, username: e.username } })
      userList({ 's_roleCode': 'expert' }).then(response => {
        this.persons = this.mergeArray(this.persons, response.data.map(e => { return { name: e.name, username: e.username } }))
      })
    })
  },
  methods: {
    close() {
      this.$emit('cancelNewOrderForm', 'refresh')
    },
    mergeArray(arr1, arr2) {
      // eslint-disable-next-line no-array-constructor
      var _arr = new Array()
      for (var i = 0; i < arr1.length; i++) {
        _arr.push(arr1[i])
      }
      for (let i = 0; i < arr2.length; i++) {
        var flag = true
        for (var j = 0; j < arr1.length; j++) {
          if (arr2[i] === arr1[j]) {
            flag = false
            break
          }
        }
        if (flag) {
          _arr.push(arr2[i])
        }
      }
      return _arr
    }
  }

}
</script>
